package com.yykj.base.server.service.impl;

import com.yykj.base.client.dto.*;
import com.yykj.base.server.converter.BaseOfficerDTOConverter;
import com.yykj.base.server.model.entity.Area;
import com.yykj.base.server.model.entity.BaseDepartment;
import com.yykj.base.server.model.entity.BaseHospitalType;
import com.yykj.base.server.model.entity.BaseOfficer;
import com.yykj.base.server.model.repository.AreaRepository;
import com.yykj.base.server.model.repository.BaseDepartmentRepository;
import com.yykj.base.server.model.repository.BaseHospitalTypeRepository;
import com.yykj.base.server.model.repository.BaseOfficerRepository;
import com.yykj.base.server.service.BaseService;
import com.yykj.base.server.service.WordService;
import com.yykj.common.annotation.LocalAddCache;
import com.yykj.common.enums.ResultEnums;
import com.yykj.common.exception.NormalException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class BaseServiceImpl implements BaseService {

    @LocalAddCache(prefix = "hospital_type")
    @Override
    public List<HospitalTypeListDTO> getHospitalType() {

        List<BaseHospitalType> baseHospitalTypeList = baseHospitalTypeRepository.findAll(Sort.by(Sort.Order.asc("code1")));

        List<HospitalTypeListDTO> result = new ArrayList<>();

        for (BaseHospitalType baseHospitalType : baseHospitalTypeList) {
            if (baseHospitalType.getCode2() == null) {
                HospitalTypeListDTO dto = new HospitalTypeListDTO();
                dto.setCode(baseHospitalType.getCode1());
                dto.setName(baseHospitalType.getName());
                dto.setList(new ArrayList<>());
                for (BaseHospitalType child : baseHospitalTypeList) {
                    HospitalTypeDTO type = new HospitalTypeDTO();
                    if (child.getCode2() != null && child.getCode1().equalsIgnoreCase(baseHospitalType.getCode1())) {
                        type.setCode(child.getCode2());
                        type.setName(child.getName());
                        dto.getList().add(type);
                    }
                }
                result.add(dto);
            }

        }

        return result;
    }

    @Override
    public List<BaseDepartmentDTO> getBaseDepartment() {
        List<BaseDepartment> list = baseDepartmentRepository.findAll(Sort.by(Sort.Order.asc("sorting")));
        List<BaseDepartmentDTO> result = new ArrayList<>();
        for (BaseDepartment baseDepartment : list) {
            BaseDepartmentDTO dto = new BaseDepartmentDTO();
            BeanUtils.copyProperties(baseDepartment, dto);
            result.add(dto);
        }
        return result;
    }

    @Override
    public BaseDepartmentDTO editBaseDepartment(BaseDepartmentDTO dto) {
        BaseDepartment department = new BaseDepartment();
        if (dto.getId() != null && dto.getId() > 0) {
            department = baseDepartmentRepository.findById(dto.getId()).orElseThrow(() -> new NormalException(ResultEnums.NOT_EXIST));
            department.setPid(dto.getPid());
            department.setSorting(dto.getSorting());
        } else {
            BeanUtils.copyProperties(dto, department);
        }

        if (dto.getName() != null) {
            WordBriefInfo wordBriefInfo = wordService.getWordBriefInfo(dto.getName());
            department.setName(dto.getName());
            department.setPy(wordBriefInfo.getPy());
            department.setWb(wordBriefInfo.getWb());
        }

        department = baseDepartmentRepository.save(department);
        BeanUtils.copyProperties(department, dto);
        return dto;
    }

    @Override
    public void deleteBaseDepartment(Integer id) {
        BaseDepartment department = baseDepartmentRepository.findById(id).orElseThrow(() -> new NormalException(ResultEnums.NOT_EXIST));
        baseDepartmentRepository.delete(department);
    }

    @LocalAddCache(prefix = "base_officer")
    @Override
    public List<BaseOfficerDTO> getBaseOfficer() {
        List<BaseOfficer> list = baseOfficerRepository.findAll(Sort.by(Sort.Order.asc("sorting")));
        return BaseOfficerDTOConverter.convert(list);
    }

    @Override
    public List<Area> listAllArea(String parentCode) {
        if(StringUtils.isEmpty(parentCode)){
            return areaRepository.findAllByLevel(0);
        }else{
            return areaRepository.findAllByParentCode(parentCode);
        }
    }




    private BaseHospitalTypeRepository baseHospitalTypeRepository;

    private BaseDepartmentRepository baseDepartmentRepository;

    private BaseOfficerRepository baseOfficerRepository;

    private WordService wordService;

    private AreaRepository areaRepository;

    @Autowired
    public void setHospitalTypeRepository(BaseHospitalTypeRepository baseHospitalTypeRepository) {
        this.baseHospitalTypeRepository = baseHospitalTypeRepository;
    }

    @Autowired
    public void setBaseDepartmentRepository(BaseDepartmentRepository baseDepartmentRepository) {
        this.baseDepartmentRepository = baseDepartmentRepository;
    }

    @Autowired
    public void setDoctorOfficerRepository(BaseOfficerRepository baseOfficerRepository) {
        this.baseOfficerRepository = baseOfficerRepository;
    }

    @Autowired
    public void setWordService(WordService wordService) {
        this.wordService = wordService;
    }

    @Autowired
    public void setAreaRepository(AreaRepository areaRepository) {
        this.areaRepository = areaRepository;
    }
}
